/*
 * chunk_service_unittest.c
 *
 *  Created on: Mar 2, 2013
 *      Author: redchen
 */

#include <lfs.h>
#include <strings.h>
#include <stdio.h>
#include <string.h>
#include <unittest.h>
#include <stddefine.h>
#include <iterator_utils.h>
#include <emalloc.h>
#include <timer.h>
#include <store_file.h>
#include <lfs_protocol.h>

#include "chunk_service.h"

int test_chunk_service_setup(void)
{
    system("data/c.10000");
    chunk_service_init();
    return 1;
}

int test_chunk_service_cleanup(void)
{
    system("data/c.10000");
    return 1;
}

void test_chunk_service_chunk_create()
{
    {
        AddChunkRequest add_chunk_req =
        { "/duoln/index.html", 10000, 64 * 1024 };
        Buffer* req_buf = buffer_new();
        add_chunk_request_serialize(&add_chunk_req, req_buf);

        Request req =
        { CHUNKSERVER_HANDLER_ADD_CHUNK, buffer_get_size(req_buf), buffer_get_data(req_buf) };
        Response resp =
        { };

        chunk_service_request(&req, &resp);

        AddChunkResponse add_chunk_resp =
        { };
        Buffer* resp_buf = buffer_create(resp.size, resp.data);
        add_chunk_response_deserialize(resp_buf, &add_chunk_resp);

        LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_SUCC), lfs_msg(add_chunk_resp.status));
    }
    {
        AddChunkRequest add_chunk_req =
        { "/duoln/index.html", 10000, 64 * 1024 };
        Buffer* req_buf = buffer_new();
        add_chunk_request_serialize(&add_chunk_req, req_buf);

        Request req =
        { CHUNKSERVER_HANDLER_ADD_CHUNK, buffer_get_size(req_buf), buffer_get_data(req_buf) };
        Response resp =
        { };

        chunk_service_request(&req, &resp);

        AddChunkResponse add_chunk_resp =
        { };
        Buffer* resp_buf = buffer_create(resp.size, resp.data);
        add_chunk_response_deserialize(resp_buf, &add_chunk_resp);

        LEMON_TEST_EQUAL_STRING(lfs_msg(LFS_FILE_IS_EXIST), lfs_msg(add_chunk_resp.status));
    }
}

void chunk_service_add_suite()
{
    UNITTEST_SUITE_INIT();
    UNITTEST_SUITE_SETUP(test_chunk_service_setup);
    UNITTEST_SUITE_CLEANUP(NULL);
    UNITTEST_SUITE_ADD_TESTCASE("test_chunk_service_chunk_create", test_chunk_service_chunk_create);
}

